<template>
  <!-- 风险管控清单 -->
  <div class="riskcontrolcard">
    <basic-container>
      <avue-crud
        :option="option"
        :table-loading="loading"
        :data="data"
        :page.sync="page"
        v-model="form"
        ref="crud"
        @search-change="searchChange"
        @search-reset="searchReset"
        @current-change="currentChange"
        @size-change="sizeChange"
        @refresh-change="refreshChange"
      >
        <!-- 左上方菜单 -->
        <template slot="menuLeft">
          <el-button
            type="warning"
            size="small"
            plain
            icon="el-icon-download"
            @click="handleExport"
          >
            导 出
          </el-button>
        </template>

        <!-- 风险等级表格自定义 -->
        <template slot="fxdj" slot-scope="scope">
          <div>
            <div
              :class="{
                fxdj_a: fxdjObj[scope.row['fxdj']] === 'A',
                fxdj_b: fxdjObj[scope.row['fxdj']] === 'B',
                fxdj_c: fxdjObj[scope.row['fxdj']] === 'C',
                fxdj_d: fxdjObj[scope.row['fxdj']] === 'D'
              }"
            >
              {{ fxdjObj[scope.row["fxdj"]] }}
            </div>
          </div>
        </template>

        <!-- 公司人员表格自定义 -->
        <template slot="companyStaff" slot-scope="scope">
          <div>{{ formatGlcjUser(scope.row["companyStaff"]) }}</div>
        </template>

        <!-- 部门人员表格自定义 -->
        <template slot="departmentStaff" slot-scope="scope">
          <div>{{ formatGlcjUser(scope.row["departmentStaff"]) }}</div>
        </template>

        <!-- 车间人员表格自定义 -->
        <template slot="workshopPersonnel" slot-scope="scope">
          <div>{{ formatGlcjUser(scope.row["workshopPersonnel"]) }}</div>
        </template>

        <!-- 班组人员表格自定义 -->
        <template slot="crew" slot-scope="scope">
          <div>{{ formatGlcjUser(scope.row["crew"]) }}</div>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { getList, getDeptList } from "@/api/saferisk/riskcontrolcard";
import { mapGetters } from "vuex";
import { getToken } from "@/util/auth";

export default {
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      data: [],
      page: {
        pageSize: this.$pageSize,
        currentPage: 1,
        total: 0
      },
      option: {
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchSpan: 5,
        searchMenuSpan: 4,
        border: true,
        selection: false,
        addBtn: false,
        viewBtn: true,
        editBtn: false,
        delBtn: false,
        refreshBtn: true,
        columnBtn: false,
        dialogClickModal: false,
        menu: false,
        column: [
          {
            label: "风险点名称",
            prop: "fxdmc",
            align: "center",
            search: true,
            labelWidth: 85
          },
          {
            label: "风险类别",
            prop: "fxfl",
            align: "center",
            type: "select",
            search: true,
            dicUrl:
              "/api/blade-system/dict-biz/dictionary?code=risk_classification",
            props: {
              label: "dictValue",
              value: "dictKey"
            }
          },
          {
            label: "所属部门",
            prop: "ssbm",
            align: "center",
            type: "tree",
            search: true,
            dicData: [],
            props: {
              label: "title"
            }
          },
          {
            label: "风险等级",
            prop: "fxdj",
            align: "center",
            search: true,
            type: "select",
            width: 80,
            addDisplay: false,
            editDisplay: false,
            viewDisplay: false,
            dicUrl: "/api/blade-system/dict-biz/dictionary?code=risk_level",
            props: {
              label: "dictValue",
              value: "dictKey"
            },
            slot: true
          },
          {
            label: "公司人员",
            prop: "companyStaff",
            align: "center",
            slot: true
          },
          {
            label: "部门人员",
            prop: "departmentStaff",
            align: "center",
            slot: true
          },
          {
            label: "车间人员",
            prop: "workshopPersonnel",
            align: "center",
            slot: true
          },
          {
            label: "班组人员",
            prop: "crew",
            align: "center",
            slot: true
          }
        ]
      },
      fxdjObj: {
        "1": "A",
        "2": "B",
        "3": "C",
        "4": "D"
      }
    };
  },
  computed: {
    ...mapGetters(["permission"])
  },
  mounted() {
    this.initData();
  },
  methods: {
    // 初始化数据
    initData() {
      var tenantId = JSON.parse(localStorage.getItem("saber-tenantId")).content;
      getDeptList(tenantId)
        .then(res => {
          const column = this.findObject(this.option.column, "ssbm");
          column.dicData = res.data.data;
        })
        .then(() => {
          this.onLoad(this.page);
        });
    },

    // 显示管理层级人员
    formatGlcjUser(userlist) {
      if (userlist === undefined || userlist.length === 0) {
        return "";
      } else {
        var users = [];
        userlist.forEach(item => {
          users.push(item.realName);
        });
        return users.join(",");
      }
    },

    // 导出
    handleExport() {
      this.$confirm("是否导出风险点辨识列表?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        let fxdmc = this.query.fxdmc === undefined ? "" : this.query.fxdmc;
        let fxfl = this.query.fxfl === undefined ? "" : this.query.fxfl;
        let ssbm = this.query.ssbm === undefined ? "" : this.query.ssbm;
        let fxdj = this.query.fxdj === undefined ? "" : this.query.fxdj;
        window.open(
          `/api/riskPoint/riskcontrol/export?${
            this.website.tokenHeader
          }=${getToken()}&fxdmc=${fxdmc}&fxfl=${fxfl}&ssbm=${ssbm}&fxdj=${fxdj}`
        );
      });
    },

    // 清空搜索条件
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },

    // 搜索条件改变事件
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },

    // 换页数
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      this.onLoad(this.page);
    },

    // 换每页显示数量
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.onLoad(this.page);
    },

    // 刷新
    refreshChange() {
      this.onLoad(this.page, this.query);
    },

    // 获取表格内容
    onLoad(page, params = {}) {
      this.loading = true;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query)
      ).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
      });
    }
  }
};
</script>

<style lang="scss" scpoed>
%fxdj_div {
  width: 27px;
  height: 27px;
  margin: 0 auto;
  color: #fff;
  font-weight: bold;
  padding-top: 2px;
}

%level_a_color {
  background-color: #ea4242;
}

%level_b_color {
  background-color: #fa9a3b;
}

%level_c_color {
  background-color: #fcd42a;
}

%level_d_color {
  background-color: #6db6eb;
}

.riskcontrolcard {
  .avue-crud {
    .avue-crud__search {
      .avue-form__row:nth-child(1) {
        .el-form-item__label {
          text-align: unset;
          width: 90px !important;
        }

        .el-form-item__content {
          margin-left: 90px !important;
        }
      }
    }
  }

  .fxdj_a {
    @extend %fxdj_div;
    @extend %level_a_color;
  }

  .fxdj_b {
    @extend %fxdj_div;
    @extend %level_b_color;
  }

  .fxdj_c {
    @extend %fxdj_div;
    @extend %level_c_color;
  }

  .fxdj_d {
    @extend %fxdj_div;
    @extend %level_d_color;
  }
}
</style>